//
//  CityViewController.m
//  LocomotiveGuards
//
//  Created by zwj on 15-11-19.
//  Copyright (c) 2015年 REDOOR. All rights reserved.
//

#import "CityViewController.h"
#import "DistrictViewController.h"
@interface CityViewController () <UIPickerViewDelegate,UIPickerViewDataSource>
{
    UIPickerView *_pickerView;
    UIButton *_btnLast;
    UIButton *_btnNext;
}

@end

@implementation CityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addTitle:@"注册-身份验证"];
    [self configView];
}

- (void)configView
{
    //所在市label
    UILabel *nameLabel = [self createLabelFrame:CGRectMake(30*W, 80*H, SCREEN_WIDTH-60*W, 30*H) title:@"所在市" font:[UIFont systemFontOfSize:26]];
    [self.view addSubview:nameLabel];
    
    _pickerView = [[UIPickerView alloc] init];
    _pickerView.center = CGPointMake(self.view.center.x, self.view.center.y+10*H);
    _pickerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    _pickerView.showsSelectionIndicator = YES;
    [_pickerView setBackgroundColor:[UIColor clearColor]];
    CALayer *viewLayer = _pickerView.layer;
    [viewLayer setBounds:CGRectMake(0, 0, SCREEN_WIDTH-80*W, SCREEN_HEIGHT-200*H)];
    _pickerView.delegate = self;
    _pickerView.dataSource = self;
    [_pickerView selectRow:self.cityArray.count/2 inComponent:0 animated:YES];
    [self.view addSubview:_pickerView];
    
    //上一步按钮
    _btnLast = [self addLastBtnTarget:self action:@selector(btnLastClicked:)];
    [self enableButton:_btnLast];
    
    //下一步按钮
    _btnNext = [self addNextBtnTarget:self action:@selector(btnNextClicked:)];
    [self enableButton:_btnNext];
}

#pragma mark - 点击事件
//上一步，返回上一个界面
- (void)btnLastClicked:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

//下一步，进入新页面
- (void)btnNextClicked:(id)sender
{
    NSInteger num = [_pickerView selectedRowInComponent:0];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[self.cityArray[num] objectForKey:@"cName"] forKey:@"cityName"];
    NSArray *selectCity = [self.cityArray[num] objectForKey:@"distArr"];
    DistrictViewController *distCtrl = [DistrictViewController new];
    distCtrl.distArray = selectCity;
    [self.navigationController pushViewController:distCtrl animated:YES];
}

#pragma mark- UIPickerView代理函数
//行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return self.cityArray.count;
}

//一共多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 40;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *pickerLabel = (UILabel *)view;
    if (!pickerLabel) {
        pickerLabel = [UILabel new];
        pickerLabel.text = [self.cityArray[row] objectForKey:@"cName"];
        pickerLabel.textAlignment = NSTextAlignmentCenter;
        pickerLabel.textColor = [UIColor whiteColor];
    }
    return pickerLabel;
}

#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
